class MultiPlayerSocket {
    constructor(playground) {
        this.playground = playground

        this.ws = new WebSocket("wss://app949.acapp.acwing.com.cn/wss/multiplayer/")
        this.start()
    }

    start() {
        this.receive()
    }

    receive() {
        let out = this
        this.ws.onmessage = function(e) {
            let data = JSON.parse(e.data)
            let uuid = data.uuid
            if(uuid === out.uuid) return false
            
            let event = data.event
            if(event === 'create player') {
                out.receive_create_player(uuid, data.username, data.photo)
            } else if(event === 'move to') {
                out.receive_move_to(uuid, data.tx, data.ty)
            } else if(event === 'shoot fireball') {
                out.receive_shoot_fireball(uuid, data.tx, data.ty, data.ball_uuid)
            } else if(event === 'attack') {
                out.receive_attack(uuid, data.attackee_uuid, data.x, data.y, data.angle, data.damage, data.ball_uuid)
            } else if(event === 'blink') {
                out.receive_blink(uuid, data.tx, data.ty)
            } else if(event === 'message') {
                out.receive_message(data.username, data.message)
            }
        }
    }

    send_create_player(username, photo) {
        let out = this
        this.ws.send(JSON.stringify({
            'uuid': out.uuid,
            'event': 'create player',
            'username': username,
            'photo': photo,
        }))
    }

    receive_create_player(uuid, username, photo) {
        let player = new Player(this.playground, this.playground.width / 2 / this.playground.scale, 0.5, 0.05, 'white', 0.15, 'enemy', username, photo)
        player.uuid = uuid
        this.playground.players.push(player)
    }

    get_player(uuid) {
        let players = this.playground.players
        for(let i = 0; i < players.length; i++)
            if(players[i].uuid === uuid)
                return players[i]
        return null
    }

    send_move_to(tx, ty) {
        let out = this
        this.ws.send(JSON.stringify({
            'uuid': out.uuid,
            'event': 'move to',
            'tx': tx,
            'ty': ty,
        }))
    }

    receive_move_to(uuid, tx, ty) {
        let player = this.get_player(uuid)
        if(player) 
            player.move_to(tx, ty)
    }

    send_shoot_fireball(tx, ty, ball_uuid) {
        let out = this
        this.ws.send(JSON.stringify({
            'event': 'shoot fireball',
            'uuid': out.uuid,
            'tx': tx,
            'ty': ty,
            'ball_uuid': ball_uuid,
        }))
    }

    receive_shoot_fireball(uuid, tx, ty, ball_uuid) {
        let player = this.get_player(uuid)
        if(player) {
            let fireball = player.shoot_fireball(tx, ty)
            fireball.uuid = ball_uuid
        }
    }

    send_attack(attackee_uuid, x, y, angle, damage, ball_uuid) {
        let out = this
        this.ws.send(JSON.stringify({
            'event': 'attack',
            'uuid': out.uuid,
            'attackee_uuid': attackee_uuid,
            'x': x,
            'y': y,
            'angle': angle,
            'damage': damage,
            'ball_uuid': ball_uuid,
        }))
    }
    
    receive_attack(uuid, attackee_uuid, x, y, angle, damage, ball_uuid) {
        let attacker = this.get_player(uuid)
        let attackee = this.get_player(attackee_uuid)
        if(attacker && attackee) {
            attackee.receive_attacked(x, y, angle, damage, ball_uuid, attacker)
        }
    }

    send_blink(tx, ty) {
        let out = this
        this.ws.send(JSON.stringify({
            'event': 'blink',
            'uuid': out.uuid,
            'tx': tx,
            'ty': ty,
        }))
    }

    receive_blink(uuid, tx, ty) {
        let player = this.get_player(uuid)
        if(player) {
            player.blink(tx, ty)
        }
    }

    send_message(username, message) {
        let out = this
        console.log('send', username, message)
        this.ws.send(JSON.stringify({
            'event': 'message',
            'uuid': out.uuid,
            'username': username,
            'message': message,
        }))
    }

    receive_message(username, text) {
        console.log('receive', username, text)
        this.playground.chat_field.add_message(username, text)
    }
}
